<?php
namespace app\mxty\controller;

use think\facade\Request;
use app\mxty\model\MemberCard as MemberCardModel;
use app\api\controller\Controller as ApiController;
use app\mxty\logic\MemberCard as MemberCardLogic;

class MemberCard extends ApiController
{
    public function getlist()
    {
        $model = new MembercardModel();
        $list = $model->getList();
        return $this->renderSuccess($list->toArray());
    }

    public function recommends()
    {
        //获取做了推荐标记的会员卡
        $list = MembercardModel::getRecommends();
        return $this->renderSuccess($list->toArray());
    }

    // 购买会员卡
    public function buy($cardId)
    {
        $res = (new MemberCardLogic)->buyCard($cardId);
        return $this->renderSuccess($res);
    }

    public function detail($cardId)
    {
        $card = (new MemberCardLogic)->getCardDetail($cardId);
        return $this->renderSuccess($card);
    }

    // 获取用户购买的会员卡列表
    public function purchased()
    {
        $list = (new MemberCardLogic)->getUserCards();
        return $this->renderSuccess($list);
    }

    // 生成二维码token
    public function getCodeToken()
    {
        $codeToken = (new MemberCardLogic)->generateCodeToken();
        return $this->renderSuccess(compact('codeToken'));
    }

    /**
     * 工作人员扫码获取会员卡信息
     * @param int $orderId 会员卡订单id
     * @param string $token 会员卡二维码token
     */
    public function checkCard($orderId, $token)
    {
        $order = (new MemberCardLogic)->checkCardInfo($orderId, $token);
        return $this->renderSuccess($order);
    }

    /**
     * 工作人员确认使用会员卡
     * @param int $orderId 会员卡订单id
     * @param string $token 会员卡二维码token
     */
    public function useCard($orderId, $token)
    {
        $order = (new MemberCardLogic)->useCard($orderId, $token);
        return $this->renderSuccess($order, '使用成功！');
    }
}
